import {isNumber} from "./validator";
import {Option} from "@/types/public";
import * as columns from "@/theme/enums/columns";
// 添加单位
export const addUnit = (num: number | string, unit = "px") => {
    return isNumber(num) ? `${num}${unit}` : num;
}
// 数字小于10 进行补零
export const fillZero = (n: number) => {
    return n < 10 ? `0${n}` : n;
}

// 获取uuid
export function guid(len = 32, firstU = true, radix: number | null = null) {
    let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    let uuid = [];
    radix = radix || chars.length;

    if (len) {
        // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
        for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
        let r;
        // rfc4122标准要求返回的uuid中,某些位为固定的字符
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';

        for (let i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guid不能用作id或者class
    if (firstU) {
        uuid.shift();
        return 'u' + uuid.join('');
    } else {
        return uuid.join('');
    }
}

// 给日期填充开始与结束时间
export function fillTime<T>(date: string | T) {
    let times = [" 00:00:00", " 23:59:59"];
    if (typeof date == "string") {
        return [date + times[0], date + times[1]]
    } else if (Array.isArray(date)) {
        return date.map((item, i) => item + times[i])
    } else {
        return date;
    }
}


// 将字符串转为小写或者大写1
export function toUpperCase(str: string, reverse: boolean | number = 0) {
    return reverse ? str.toLowerCase() : str.toUpperCase();
}

//将对象转为Option数组
export function objToArr<T extends object, V>(obj: T, convert?: Function): Option<V>[] {
    return Object.entries(obj).map(([k, v]) => {
        return {
            label: convert ? convert(v) : v,
            value: k as V
        }
    })
}

// Option数组转对象
export function arrToMaps<T extends Record<string, string>>(arr: T[], valueKey: string, labelKey: string = "label"): Record<string, string> {
    return arr.reduce((prv: Record<string, string>, cur: Record<string, string>) => {
        prv[cur[valueKey]] = cur[labelKey];
        return prv;
    }, {})
}

/*
 * @Author: EDY
 * @Date: 2024/5/30
 * @LastEditors: EDY
 * @Description: 数字格式化
 * @Params:
 */
export function numberFormatter(num: string | number, digits = 2) {
    if (!isNumber(num)) return "";
    if (typeof num === 'string') num = parseFloat(num)
    const si = [
        // {
        // 	value: 1E18,
        // 	symbol: 'E'
        // },
        // {
        // 	value: 1E15,
        // 	symbol: 'P'
        // },
        // {
        // 	value: 1E12,
        // 	symbol: 'T'
        // },
        // {
        // 	value: 1E9,
        // 	symbol: 'G'
        // },
        // {
        // 	value: 1E6,
        // 	symbol: 'M'
        // },
        {
            value: 1E4,
            symbol: 'w'
        },
        {
            value: 1E3,
            symbol: 'k'
        }
    ]
    for (let i = 0; i < si.length; i++) {
        if (num >= si[i].value) {
            return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
        }
    }
    return num.toString()
}

/*
 * @Author: EDY
 * @Date: 2024/6/6
 * @LastEditors: EDY
 * @Description: 提取固定字符串
 * @Params:
 */
export function extractStr(str: string, fixedPrefix: string) {
    const regex = new RegExp(`(${fixedPrefix})(\\w+)`);
    const result = str.match(regex);
    return result ? [result[1], result[2]] : null;
}